package com.jhx.mall.app.servlet;

import com.jhx.mall.app.service.DtsAddressService;
import com.jhx.mall.app.service.impl.DtsAddressServiceImpl;
import com.jhx.mall.entity.DtsAddress;
import com.jhx.mall.util.BaseServlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "DtsAddress", value = "/api/user/address")
public class DtsAddressSerlvet extends BaseServlet {

    private DtsAddressService dtsAddressService = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        dtsAddressService = new DtsAddressServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、创建out对象
        PrintWriter out = response.getWriter();
        // 2、获取opr请求参数
        String opr = request.getParameter("opr");

        if (opr.equals("list")){
            int userId = Integer.parseInt(request.getParameter("userId"));
            List<DtsAddress> list = dtsAddressService.selectAddress(userId);
            if (list != null){
                ok(request,response,list);
            }else {
                error(request,response,list);
            }
        } else if (opr.equals("insert")){
            DtsAddress address = new DtsAddress();
            address.setUserId(Integer.parseInt(request.getParameter("userId")));
            address.setName(request.getParameter("name"));
            address.setMobile(request.getParameter("mobile"));
            address.setAddress(request.getParameter("address"));
            address.setProvinceId(Integer.parseInt(request.getParameter("provinceId")));
            address.setCityId(Integer.parseInt(request.getParameter("cityId")));
            address.setAreaId(Integer.parseInt(request.getParameter("areaId")));
            address.setIsDefault(Integer.parseInt(request.getParameter("isDefault")));
            if (dtsAddressService.addAddress(address)>0){
                ok(request,response,address);
            }else {
                error(request,response,address);
            }
        } else if (opr.equals("update")){
            DtsAddress address = new DtsAddress();
            address.setId(Integer.parseInt(request.getParameter("id")));
            address.setName(request.getParameter("name"));
            address.setMobile(request.getParameter("mobile"));
            address.setAddress(request.getParameter("address"));
            address.setProvinceId(Integer.parseInt(request.getParameter("provinceId")));
            address.setCityId(Integer.parseInt(request.getParameter("cityId")));
            address.setAreaId(Integer.parseInt(request.getParameter("areaId")));
            if (dtsAddressService.upAddress(address)>0){
                ok(request,response,address);
            }else {
                error(request,response,address);
            }
        } else if (opr.equals("del")){
            if (dtsAddressService.delAddress(Integer.parseInt(request.getParameter("id")))>0){
                ok(request,response,null);
            }else {
                error(request,response,null);
            }
        }

    }
}
